package com.tangcco.lockwell.controller.admin;

import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.DataDictionary;
import com.tangcco.lockwell.domain.ExamPaper;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.service.DataDictionaryService;
import com.tangcco.lockwell.service.ExamPaperService;
import com.tangcco.lockwell.viewmodel.admin.exam.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController("AdminExamPaperController")
@RequestMapping("/api/admin/examPaper")
public class ExamPaperController {
    private final ExamPaperService examPaperService;
    private final DataDictionaryService dataDictionaryService;
    @Autowired
    WebContext webContext;

    @Autowired
    public ExamPaperController(ExamPaperService examPaperService, DataDictionaryService dataDictionaryService) {
        this.examPaperService = examPaperService;
        this.dataDictionaryService = dataDictionaryService;
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaper>> pageList(@RequestBody ExamPaperPageRequestVM model) {
        PageInfo<ExamPaper> pageInfo = examPaperService.page(model);
        return RestResponse.ok(pageInfo);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public RestResponse<List<ExamPaper>> list( ExamPaper model) {
        List<ExamPaper> list = examPaperService.selectList(model);
        return RestResponse.ok(list);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse<EPaperEditRequestVM> edit(@RequestBody ExamPaperEditRequestVM model) throws ParseException {
        User user = webContext.getCurrentUser();
        ExamPaper examPaper = examPaperService.savePaperFromVM(model, user);
        EPaperEditRequestVM newVM = examPaperService.examPaperToVM(examPaper.getPaperId());
        return RestResponse.ok(newVM);
    }

    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<EPaperEditRequestVM> select(@PathVariable Integer id) throws ParseException {
        EPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    }
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        ExamPaper examPaper = examPaperService.selectById(id);
        examPaperService.deletePaper(examPaper);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/pageVm", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperVM>> pageListVm(@RequestBody ExamPaperPageRequestVM model) {
        PageInfo<ExamPaperVM> pageInfo = examPaperService.pagevm(model);
        return RestResponse.ok(pageInfo);
    }

    //获取所有试卷类型
    @PostMapping("/paperType")
    public RestResponse<List<DataDictionary>> type() {
        List<DataDictionary> paperType = dataDictionaryService.selectDataDictionaryByCategoryCode("paper_type");
        return RestResponse.ok(paperType);
    }
    @RequestMapping(value = "/examPaperCount", method = RequestMethod.POST)
    public RestResponse<Long> getExamPaperCount() {
        long count = examPaperService.getExamPaperCount();
        return RestResponse.ok(count);
    }

    @RequestMapping(value = "/AddAiPaper", method = RequestMethod.POST)
    public RestResponse<Integer> AddAiPaper(@RequestBody ExampaperAddAIVM model) {
        ExamPaperEditRequestVM model2 = examPaperService.ExamPaperAddAi(model);
        User user = new User();
        user.setUserId(0);
        ExamPaper examPaper = examPaperService.savePaperFromVM(model2, user);
        return RestResponse.ok(examPaper.getPaperId());
    }

}
